package com.qitmiaojie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.User;

import com.qitmiaojie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class UserController {
/*
    @Autowired
    private UserService userService;


    @GetMapping("/user")
    public Result GetUserInfo(Principal principal){
        User user = userService.getByUsername(principal.getName());

        return new Result(200,"查询成功",user);
    }
    *//*users?Name=&CurrentPage=1&Size=10*//*

    *//**
     * 分页查询所有
     * @param Name
     * @param CurrentPage
     * @param Size
     * @return
     *//*
    @GetMapping("/users")
    public Result users(String Name, Integer CurrentPage, Integer Size){
        log.info("{},{},{}",Name,CurrentPage,Size);
        return userService.selectByUsers(Name,CurrentPage,Size);
    }

    *//**
     * 添加用户
     * @param user
     * @return
     *//*
    @PostMapping("/user")
    public Result addUser(@RequestBody User user){
        log.info(user.toString());
        userService.save(user);
        return new Result(200,"添加成功",null);
    }

    *//**
     * 删除用户
     * @param id
     * @return
     *//*
    @DeleteMapping("/user/{id}")
    public Result delUser(@PathVariable Integer id){
        log.info("{}",id);
        userService.removeById(id);
        return new Result(200,"删除成功",null);
    }

    *//**
     * 批量删除
     * @param ids
     * @return
     *//*
    @DeleteMapping("/user")
    public Result delUser(@RequestParam List<Integer> ids){
        log.info("{}",ids);
        userService.removeByIds(ids);
        return new Result(200,"删除成功",null);
    }

    *//**
     * 根据id查询用户
     * @param id
     * @return
     *//*
    @GetMapping("/user/{id}")
    public Result findById(@PathVariable Integer id){
        return Result.success(userService.getById(id));
    }

    *//**
     * 修改
     * @param user
     * @return
     *//*
    @PutMapping("/user")
    public Result updateUser(@RequestBody User user){
        log.info(user.toString());
        userService.updateById(user);
        return Result.success("修改成功");
    }
    @PutMapping("/user/{id}")
    public Result updateUserRole(@PathVariable Integer id,@RequestBody Map<String, Object> ids){
        System.out.println(ids.values());
        List<String> listIds = (List<String>)ids.get("ids");
        List<Integer> list = listIds.stream().map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        }).collect(Collectors.toList());
        return userService.updateByIdAndUserRole(id,list);
    }

    @GetMapping("/user/role/{id}")
    public Result getByIdAndRole(@PathVariable Integer id){
        return userService.selectByIdRole(id);
    }*/
    @Autowired
    private UserService userService;
    @GetMapping("/user")
    public Result GetUserInfo(Principal principal){
        return userService.QueryUserByUserName(principal.getName());
    }
    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping("/users")
    public Result GetUsersInfo(@RequestParam(value = "Name",required = false) String Name
            ,@RequestParam(value = "CurrentPage",required = false,defaultValue = "1") Integer CurrentPage
            ,@RequestParam(value = "Size",required = false,defaultValue = "10") Integer Size){
        return userService.QueryUsers(Name,CurrentPage,Size);
    }
    //    @PreAuthorize("hasAuthority('sys:user:save')")
    @PostMapping("/user")
    public Result AddUser(@RequestBody User user){
        //System.out.println(user);
        return userService.AddUser(user);
    }
    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping("/user/{id}")
    public Result QueryUserById(@PathVariable Integer id){
        return userService.QueryUserById(id);
    }
    @PreAuthorize("hasAuthority('sys:user:update')")
    @PutMapping("/user")
    public Result UpdateUser(@RequestBody User user){
        System.out.println("-----------"+user);
        return userService.UpdateUser(user);
    }
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @DeleteMapping("/user/{id}")
    public Result DeleteUser(@PathVariable Integer id){
        return userService.DeleteUser(id);
    }
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @DeleteMapping("/user")
    public Result DeleteManyUser(Integer[] ids){
        return userService.DeleteManyUser(ids);
    }
    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping("/user/role/{id}")
    public Result QueryUserRole(@PathVariable Integer id){
        return userService.QueryUserRole(id);
    }
    @PreAuthorize("hasAuthority('sys:user:update')")
    @PutMapping("/user/{id}")
    public Result UpdateUserAuth(@PathVariable Integer id, @RequestBody Map<String,Object> map ){
        List<Integer> ids=(List<Integer>) map.get("ids");
        //System.out.println(ids);
        Map<String,Object> objectMap=new HashMap<>();
        objectMap.put("id",id);
        objectMap.put("ids",ids);
        //return null;
        return userService.UpdateUserAuth(objectMap);
    }
    @PreAuthorize("hasAuthority('sys:user:repass')")
    @PutMapping("/user/password/{id}")
    public Result ResetPassword(@PathVariable Integer id){
        return userService.ResetPassword(id);
    }
    @PreAuthorize("hasAuthority('sys:user:date')")
    @PutMapping("/user/newpassword")
    public Result ChangePassword(Principal principal, String currentpassword, String password){
        System.out.println(currentpassword+"----"+password);
        return  userService.ChangePassword(principal.getName(),currentpassword,password);
    }
}
